/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class Probe extends WiringNode implements WiringOutput{
    private Vector<WiringConsumer> consumers = new Vector();

    private float [] data;
    private String [] titles;
    int index;

    SensorSource obs;

    private float outVal = 0;
    private int consumerIndex = 0;  //this is a temp variable to hold a pointer to
    //the consumer until we can be sure the consumer
    //has been instantiated
    public int type;               //does this point to a node or a constraint as an output?
    Vector<ConnectionInfo> connections = new Vector();


    public Probe(SensorSource obs, int index,  TrueTypeFont font){ 
        this.index = index;
        data = obs.getDataArray();
        titles = obs.getTitleArray();
        this.font = font;
        this.obs = obs;

    }

    public void setConsumer(WiringConsumer consumer) {
        consumers.add(consumer);
    }

    @Override
    public void lineToConsumer() {

    }

    public void hookUp(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        for (ConnectionInfo ci : connections) {
            if (ci.index != -1) {
                if (ci.type == 0) {
                    setConsumer((WiringConsumer) wiring.elementAt(ci.index));
                } else if (ci.type == 1) {
                    setConsumer((WiringConsumer) nodes.elementAt(ci.index));
                } else if (ci.type == 2) {
                    setConsumer((WiringConsumer) constraints.elementAt(ci.index));
                }

            }
        }

    }

    @Override
    public void evaluate() {
        outVal = data[index];
        for (WiringConsumer c : consumers) {
            c.setValue(outVal);
        }

        finished = true;
    }

  

    public Probe(String[] line, Vector<Node> nodes, TrueTypeFont font) {
        this((SensorSource)nodes.elementAt(Integer.parseInt(line[1])),
                Integer.parseInt(line[2]),
                font);

        for (int i = 3; i < line.length; i += 2) {
            connections.add(new ConnectionInfo(Integer.parseInt(line[i]),
                    Integer.parseInt(line[i + 1])));

        }
 
 
    }




    public String pickle(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        String ret = "p,"
                + nodes.indexOf(obs) + ","
                + index + ",";
        
        for (WiringConsumer wc : consumers) {
            if (wc instanceof WiringNode) {
                ret += 0 + "," + wiring.indexOf(wc) + ",";
            } else {
                ret += 1 + "," + constraints.indexOf(wc) + ",";
            }
        }
        return ret;
    }

    public void draw() {
        super.draw();

        GL11.glColor4f(parent.TEXT[0], parent.TEXT[1], parent.TEXT[2], parent.TEXT[3]);
        //parent.font.drawString(locX + 5, locY, title, 1, 1);
        font.drawString(locX + 5, locY, titles[index] + ": " + data[index], 1f, 1f);

    }




}
